<template>
  <div class="detail">
    <el-card>
      <el-button @click="toPrint">打印页面</el-button>
      <!-- tabs -->
      <el-tabs v-model="activeName">
        <el-tab-pane label="登录账户设置" name="first">
          <!-- 表单 -->
          <el-form class="myform" :model="form" label-width="80px">
            <el-form-item label="姓名">
              <el-input v-model="form.username"></el-input>
            </el-form-item>
            <el-form-item label="密码">
              <el-input v-model="form.password" type="password"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="update" type="primary">更新</el-button>
              <el-button>取消</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情" name="second">
          <userInfo></userInfo>
        </el-tab-pane>
        <el-tab-pane label="岗位信息" name="third">
          <jobInfo></jobInfo>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { getEmployeesApi, setEmployeesApi } from '@/api/employees'
export default {
  data () {
    return {
      activeName: 'first',
      // 登录账户信息
      form: {
        username: '',
        password: ''
      },
      // 当前操作用户的 id
      id: this.$route.params.id
    }
  },
  methods: {
    // 跳转到打印页面
    toPrint () {
      // type 用来确定显示哪个结构（员工信息 & 岗位信息）
      const type =
        this.activeName === 'first' || this.activeName === 'second'
          ? 'info'
          : 'job'
      this.$router.push(`/employees/print/${this.id}?type=${type}`)
    },
    // 更新数据
    async update () {
      await setEmployeesApi(this.form)
      this.$message.success('修改成功')
      // 跳转到员工列表
      this.$router.push('/employees')
    },
    // 得到用户信息
    async getUserInfo () {
      const res = await getEmployeesApi(this.id)
      // 保存用户信息
      this.form = { ...res.data }
    }
  },
  created () {
    // 得到用户信息
    this.getUserInfo()
  },
  components: {
    userInfo: () => import('../components/user-info.vue'),
    jobInfo: () => import('../components/job-info.vue')
  }
}
</script>

<style lang="scss" scoped>
.detail {
  margin: 20px;
  .myform {
    width: 40%;
    margin: 40px 0px 0px 200px;
  }
}
</style>
